﻿using System;
using System.Collections.Generic;
using System.Text;
using BIS.Portal.Core.Base;
using BIS.Portal.Core.Global;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Portal.Modules.CMS.BO;
using BIS.Utility.Converter;

namespace BIS.Portal.Modules.CMS
{
    public partial class CmsSiteMap : ControlBase
    {
        #region Get data settings
        public string ClassNamRoot
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsSiteMap.txtClassRoot, PortalContext.LanguageAlias); }
        }
        public string ClassNamChild
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsSiteMap.txtClassChild, PortalContext.LanguageAlias); }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<ClientCmsMenuEnity> listSiteMapVN = ClientCmsMenuBO.GetCategoriesForMenu(1);
            ltrSiteMapVN.Text = GenerateSiteMap(0, ClassNamRoot, 1, ref listSiteMapVN);
            IList<ClientCmsMenuEnity> listSiteMapEN = ClientCmsMenuBO.GetCategoriesForMenu(2);
            ltrSiteMapEN.Text = GenerateSiteMap(0,ClassNamRoot, 2, ref listSiteMapEN);
        }
        private string GenerateSiteMap(int parentCategoryId, string className, int language, ref IList<ClientCmsMenuEnity> list)
        {
            StringBuilder sb = new StringBuilder();

            if (list.Count <= 0)
            {
                sb.AppendLine("");
            }
            else
            {
                List<ClientCmsMenuEnity> childCategories = new List<ClientCmsMenuEnity>();

                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].ParentCategoryId == parentCategoryId)
                    {
                        childCategories.Add(list[i]);
                    }
                }
                for (int i = 0; i < childCategories.Count; i++)
                {
                    list.Remove(childCategories[i]);
                }

                if (childCategories.Count < 1)
                {
                    sb.AppendLine("");
                }
                else
                {
                    sb.AppendLine("<ul" + (parentCategoryId == 0 ? " class=\"" + className + "\"" : "") + ">");
                    for (int i = 0; i < childCategories.Count; i++)
                    {
                        #region Url Của Category
                        string url = url = WebHelpers.GetLink(ModuleQueryStringKey.UrlNhomTin, childCategories[i].CategoryPageAlias,
                            new string[] { ModuleQueryStringKey.CategoryId, ModuleQueryStringKey.CategoryName },
                            new string[] { childCategories[i].CategoryId.ToString(), Functions.UnicodeToKoDauAndGach(childCategories[i].CategoryName) });
                        if (childCategories[i].CategoryType == 2)
                        {
                            ClientCmsArticleIdByCategoryIdEntity objArticle =
                                ClientCmsArticleBO.GetArticleIdByCategoryId(language,
                                                                            childCategories[i].CategoryId);
                            if (objArticle != null)
                            {
                                url = WebHelpers.GetLink(ModuleQueryStringKey.UrlLinkChiTiet, objArticle.PageAlias,
                                                    new string[]
                                            {
                                           ModuleQueryStringKey.CategoryId,
                                           ModuleQueryStringKey.ArticleId,
                                           ModuleQueryStringKey.CategoryName,
                                           ModuleQueryStringKey.ArticleTile
                                           },
                                                    new string[]
                                            {
                                           Convert.ToString(childCategories[i].CategoryId),
                                           Convert.ToString(objArticle.ArticleId),
                                           Functions.UnicodeToKoDauAndGach(childCategories[i].CategoryName),
                                           Functions.UnicodeToKoDauAndGach(objArticle.ArticleTitle)
                                           }
                                );
                            }
                        }
                        #endregion

                        sb.AppendLine("<li><a href='" + url + "'>" + childCategories[i].CategoryName + "</a>");
                        sb.AppendLine(GenerateSiteMap(childCategories[i].CategoryId, ClassNamChild, language, ref list));
                        sb.AppendLine("</li>");
                    }

                    sb.AppendLine("</ul>");
                }
            }
            return sb.ToString();
        }
    }
}